const UserList = require('../model/user.json');

import Mock from 'mockjs';

/** @method 用户登录 */
const login = {
	url: '/login',
	type: 'post',
	response: (req) => {
		const { tel } = JSON.parse(req.body);
		const user = UserList.find((item) => item.tel === tel);
		if (user) {
			return user;
		} else {
			// 这里可以编写注册用户的逻辑
			return {
				code: -1,
				msg: '用户不存在',
			};
		}
	},
};

/**
 * @method 获取用户信息
 */
const getUserInfo = {
	url: '/getUserInfo',
	type: 'post',
	code: 200,
	response: (req) => {
		const { id } = JSON.parse(req.body);
		const user = UserList.find((item) => item.id == id);
		if (!user) {
			return {
				code: 500,
				msg: '获取不到用户信息',
			};
		}
		return user;
	},
};

/**
 * @method 获取用户地址列表
 * @returns {
 *  id {String} 地址id
 *  tel {String} 联系电话
 *  usedAddress {Boolean} 是否常用地址
 *  address {String} 地址
 * }
 */
const getUserAddressList = {
	url: '/getUserAddressList',
	type: 'get',
	code: 200,
	response: () => {
		return {
			id: Mock.mock('@increment'),
			tel: Mock.mock(/^1[3456789]\d{9}$/),
			usedAddress: Mock.mock('@boolean'),
			address: Mock.mock('@county(true)').split(' '),
		};
	},
};

/**
 * @method 修改用户地址
 */
const putUserAddressItem = {
	url: '/putUserAddressItem',
	type: 'put',
	code: 200,
	response: (req) => {
		const { id, tel, usedAddress, address } = JSON.parse(req.body);
		const user = UserList.find((item) => item.id === id);
		if (!user) {
			return {
				code: 500,
				msg: '用户不存在',
			};
		} else {
			user.id = id;
			user.tel = tel;
			user.usedAddress = usedAddress;
			user.address = address;
			return {
				code: 200,
				msg: '修改成功',
			};
		}
	},
};

/** @method 获取用户饮食爱好 */
const getUserDietList = {
	url: '/getUserDietList',
	type: 'get',
	code: 200,
	response: () => {
		return {
			msg: '暂时没想好，等组件好再修改请求返回数据',
		};
	},
};

export default [login, getUserInfo, getUserAddressList, putUserAddressItem, getUserDietList];
